package com.damon.log;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.SimpleDateFormat;

import javax.swing.JTextArea;

import org.slf4j.Logger;

import com.damon.entity.ApiEntity;
import com.damon.entity.PrintDeviceEntity;
import com.damon.utils.GlobalOjbectUtil;
import com.damon.window.WcsWindow;
 
/**
 * ps:一个日志对象，封装了logback和java可视化界面的日志
 * @author 风蚕
 *
 */
public class WcsLog {
	private static WcsWindow wcsWindow = GlobalOjbectUtil.getWindow();

//	private PrintDeviceEntity deviceInfo;//用于获取不同url的描述信息
	private ApiEntity apiInfo;//用于获取不同url的描述信息
	private Logger sourceLogger;
	private String desc;
	/**
	 * ps:包含设备号
	 * @param sourceLogger
	 * @param wcsDvcEntity
	 */
	public WcsLog(Logger sourceLogger, ApiEntity apiInfo) {
		this.sourceLogger = sourceLogger;
		this.apiInfo = apiInfo;
		this.desc = apiInfo.getConfigDesc();
	}
	/**
	 * ps:没有设备号
	 * @param sourceLogger
	 */
	public WcsLog(Logger sourceLogger) {
		this.sourceLogger = sourceLogger;
	}

	/**
	 * ps： 日志级别中info及以上的日志，分别在控制台、java视图、日志文件中输出
	 */
	public void info(String msg) {
		printToWindow(msg,"info");
		boolean isSelected = wcsWindow.getLogToFileCheckBox().isSelected();
		if (isSelected) {
			if(apiInfo==null) {
				sourceLogger.info(msg);
			}else {
				sourceLogger.info("[url_desc:"+desc+"] "+msg);
			}
			
		}
	}

	/**
	 * ps：日志级别中erroe的日志，分别在控制台、java视图、日志文件中输出
	 * 	不带异常信息
	 */
	public void error(String msg) {
		printToWindow(msg,"error");
		
		boolean isSelected = wcsWindow.getLogToFileCheckBox().isSelected();
		//根据是否写入日志的单选框，判断是否向文件中输入日志
		if (isSelected) {
			if(apiInfo==null) {
				sourceLogger.error(msg);
			}else {
				sourceLogger.error("[url_desc:"+desc+"] "+msg);
			}
		}
	}
	
	/**
	 * ps：日志级别中error的日志，分别在控制台、java视图、日志文件中输出
	 * 	带异常信息
	 */
	public void error(String msg,Throwable t) {
		printToWindow(msg+"-"+t.getMessage(),"error");
		/*向java视图输出异常信息*/
//		StringWriter stringWriter= new StringWriter(); 
//        PrintWriter writer= new PrintWriter(stringWriter); 
//        t.printStackTrace(writer); 
//        StringBuffer buffer= stringWriter.getBuffer(); 
//        printToWindow(buffer.toString(),"error");
        
		boolean isSelected = wcsWindow.getLogToFileCheckBox().isSelected();
		//根据是否写入日志的单选框，判断是否向文件中输入日志
		if (isSelected) {
			if(apiInfo==null) {
				sourceLogger.error(msg,t);
			}else {
				sourceLogger.error("[url_desc:"+desc+"] "+msg,t);
			}
		}
	}
	


	/**
	 * 打印到java可视化界面
	 * 
	 * @param msg
	 */
	private void printToWindow(String msg,String level) {
		JTextArea textArea = wcsWindow.getTextArea();
		String apiName;
		SimpleDateFormat dataFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
		String format = null;
		if (apiInfo == null) {
			apiName = "空";
			format = String.format("%s [%s] [%s] %s\n", dataFormat.format(System.currentTimeMillis()),
					Thread.currentThread().getName(),level, msg);
		} else {
			apiName = desc;
			format = String.format("%s [%s] [%s] [url_desc:%s] "
					+ "%s\n", dataFormat.format(System.currentTimeMillis()),
					Thread.currentThread().getName(), level, apiName, msg);
		}
		if (textArea.getLineCount() > 5000) {
			textArea.setText("");
		}

		textArea.append(format);
//        textArea.append(formatter.format(System.currentTimeMillis()) + "    ["
//                + Thread.currentThread().getName() + "]" +"    ["+"008"+"]    " + msg
//                + "\r\n");
	}
}
